
cd "/Users/chuangchen/Library/CloudStorage/OneDrive-UniversityofPittsburgh/LASA/Datos"


use "/Users/chuangchen/Library/CloudStorage/OneDrive-UniversityofPittsburgh/LASA/Datos/all_countries.dta", clear

*correct legis in Brazil
replace legis=2007 if wave==1 & pais=="Brazil"
replace legis=2010 if wave==2 & pais=="Brazil"


drop sq*
drop av*
replace ID1 = . if ID1 >10
replace ID2 = . if ID2 >10


bysort pais partido legis: gen N_party_member=_N
tab pais if N_party_member < 8
* do not drop at the individual level
*drop if N_party_member<8 

lab var N_party_member "Party Size"


global variable2 ="val_ssm val_abortion val_drugs religious rel_catholic rel_evangelical rel_other education female  eco_regulated eco_education_u  eco_employment eco_companies eco_wellbeing eco_health eco_pensions eco_prices eco_education_p eco_education_s eco_housing eco_unemployment eco_environment eco_necessity eco_tax eco_private val_religious val_divorce eco_education_ps eco_tax2 eco_natural eco_inequality ID1 ID2"

foreach x in $variable2 {
	egen sq_`x'=sd(`x'), by(pais wave partido)
}


version 16

* Summary parties
foreach x in $country {
	tab pais if   pais_n==`x'
	sum  pais if pais_n==`x'
	
	tab partido wave if   pais_n==`x'

}
* Missing values?
global variable1 ="eco_regulated eco_education_u eco_employment eco_companies eco_wellbeing eco_health eco_pensions eco_prices eco_education_p eco_education_s eco_housing eco_unemployment eco_environment eco_necessity eco_tax eco_private eco_education_ps eco_tax2 eco_natural eco_inequality"
global variable2 ="val_abortion val_ssm val_drugs val_religious val_divorce val_imm val_legalage val_death  val_guns"

// version 16 needed
* Summary of all questions
* the option "contents" of table is not supported in new STATA; the new option is "statistic"
*net describe dm31, from(http://www.stata.com/stb/stb26)
*net install dm31
version 16
foreach x in $variable1 $variable2  {
	sum  `x' 
	egen nmis_`x'=rmiss2(`x')
	sum  nmis_`x'
	table pais wave , contents(mean `x' sd `x' n `x' mean nmis_`x')
	
}
foreach x in $variable1 $variable2  {
	sum  nmis_`x'	
}

lab var val_abortion "Pro-Abortion"
lab var eco_regulated "Pro-Market"
lab var eco_employment "Pro-employment"
lab var eco_pensions "Pro-pensions"

lab var sq_val_abortion"Disp Pro-Abortion"
lab var sq_eco_regulated "Disp Pro-Market"
lab var sq_eco_employment "Disp Pro-employment"
lab var sq_eco_pensions "Disp Pro-pensions"

lab var ID1 "Ideology Legis"
lab var ID2 "Ideology Party"


lab var far_right "Far Right"
lab var far_left "Far Left"

lab var religious "Religious"
lab var education "Education"
lab var female "Female"
lab var age "Age"

******************************************************************************
* Graph setting
******************************************************************************
*/ 
{
grstyle clear
set scheme s2color
grstyle init
grstyle set plain, box
grstyle color background white
grstyle set color dknavy
grstyle yesno draw_major_hgrid yes
grstyle yesno draw_major_ygrid yes
grstyle color major_grid gs8
grstyle linepattern major_grid dot
*grstyle set legend 4, box inside
grstyle color ci_area gs12%50
 graph set window fontface "Georgia"
}
*/ 

* recode moral and economic to trichotomous
* moral conservative
gen pro_abortion2= 0 if val_abortion <=3
* moral middle
replace pro_abortion2=1 if val_abortion <=7 & val_abortion>3
* moral progressive
replace pro_abortion2=2 if val_abortion >7
replace pro_abortion2=. if val_abortion >10
* eco conservative, higher value of eco_regulated means more pro-market
gen pro_regulation2= 0 if eco_regulated >7 
* eco middle
replace pro_regulation2=1 if eco_regulated <=7 & eco_regulated>3
* eco progressive
replace pro_regulation2=2 if eco_regulated <=3
replace pro_regulation2=. if eco_regulated >10

gen eco_moral2 = 1 if pro_abortion2 == 0 & pro_regulation2==0
replace eco_moral2 = 2 if pro_abortion2 == 0 & pro_regulation2==1
replace eco_moral2 = 3 if pro_abortion2 == 0 & pro_regulation2==2
replace eco_moral2 = 4 if pro_abortion2 == 1 & pro_regulation2==0
replace eco_moral2 = 5 if pro_abortion2 == 1 & pro_regulation2==1
replace eco_moral2 = 6 if pro_abortion2 == 1 & pro_regulation2==2
replace eco_moral2 = 7 if pro_abortion2 == 2 & pro_regulation2==0
replace eco_moral2 = 8 if pro_abortion2 == 2 & pro_regulation2==1
replace eco_moral2 = 9 if pro_abortion2 == 2 & pro_regulation2==2
lab define eco_moral2_lab 1 "McEc" 2 "McEm" 3 "McEp" 4 "MmEc" 5 "MmEm" 6 "MmEp" 7 "MpEc" 8 "MpEm" 9 "MpEp"
lab value eco_moral2 eco_moral2_lab


lab define pro_abort_lab 0 "Moral Conservative" 1 "Moral Middle" 2 "Moral Progressive"
lab value pro_abortion2 pro_abort_lab

lab define pro_regu_lab 0 "Eco Conservative" 1 "Eco Middle" 2 "Eco Progressive"
lab value pro_regulation2 pro_regu_lab

gen center = 1 if ID1>3 & ID1<8
replace center =0 if ID1~=. & center~=1

lab define pro_abortion1_lab 0 "Moral C" 1 "Moral P"
lab value pro_abortion pro_abortion1_lab

lab define pro_regulation1_lab 0 "Eco C" 1 "Eco P"
lab value pro_regulation pro_regulation1_lab

gen eco_moral = "Conservative" if pro_abortion==0 & pro_regulation==0
replace eco_moral = "Progressive" if pro_abortion==1 & pro_regulation==1
replace eco_moral = "EpMc" if pro_abortion==0 & pro_regulation==1
replace eco_moral = "EcMP" if pro_abortion==1 & pro_regulation==0

gen party_family = 3 if far_right==1
replace party_family = 1 if far_left==1
replace party_family = 2 if far_right!=1 & far_left!=1
replace party_family = . if far_right==. | far_left==.
label define party_family_lab 1 "Far-Left" 2 "Centrist" 3 "Far-Right"
label value party_family party_family_lab

*exporting the big table
bysort pais party_family eco_moral2: gen N_eco_moral2=_N
bysort pais: gen N_country=_N
bysort pais party_family: gen N_party_family_pais=_N
*bysort pais eco_moral2 party_family: keep if _n==1
*Do not include the last line if not exporting the excel

gen issue_party_family = N_eco_moral2/N_party_family_pais

bysort pais party_family eco_moral2: gen num=_n
*keep if num==1

*drop if eco_moral2==.
*drop num
*export excel pais party_family eco_moral2 N_party_family_pais N_eco_moral2  issue_party_family N_country using "/Users/chuangchen/Library/CloudStorage/OneDrive-UniversityofPittsburgh/Pela project/new_data.xls", firstrow(variables) replace



{
* Eco
{
global variable ="val_abortion eco_regulated  eco_employment  eco_pensions"

* calculate SD
foreach x in $variable {
	reg   ID1_std `x'   i.wave i.pais_n
	estimates store `x'
}


gen new_party = 1 if age_party<10
replace new_party = 0 if age_party>=10


label value party_family party_family_lab

gen party_family2 = 3 if far_right2==1
replace party_family2 = 1 if far_left2==1
replace party_family2 = 2 if far_right2!=1 & far_left2!=1
replace party_family2 = . if far_right2==. | far_left2==.
label value party_family2 party_family_lab


gen age_party_recode = 1 if age_party<10
replace age_party_recode = 0 if age_party>=10

label define age_party_lab 0 "Old Party (>=10 years)" 1 "New Party (<10 years)"
label value age_party_recode age_party_lab


bysort pais partido wave: egen mean_abortion = mean(val_abortion)

replace religious=0 if religious==2



*************************************
encode eco_moral, gen (eco_moral_code)
save "all_countries2.dta", replace
}
}

